package com.exp.base.util;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;
import org.springframework.util.Assert;

public class BeanUtils extends org.apache.commons.beanutils.BeanUtils{
	static {
		DateConverter dateConverter = new DateConverter();
		dateConverter.setPatterns(new String[] {"yyyy-MM-dd HH:mm:ss", "yyyyMMddHHmmss", "yyyy/MM/dd HH:mm:ss", "yyyy", "yyyy-MM", "yyyyMM", "yyyy/MM", "yyyy-MM-dd", "yyyyMMdd", "yyyy/MM/dd" });
		ConvertUtils.register(dateConverter, Date.class);
	}

    public static Map<String,Object> beanToMap(Object bean){
        Map map = null;
		try {
			map =  describe(bean);
		} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
			ExceptionUtils.throwRuntimeException(e);
		}
        return new HashMap<String,Object>(map);
    }

    public static void mapToBean(Object bean,Map properties){
		Assert.notNull(bean, "bean 不能为空");
    	try {
			populate(bean, properties);
		} catch (IllegalAccessException | InvocationTargetException e) {
			ExceptionUtils.throwRuntimeException(e);
		}
    }

	
}
